R: not only for stats

R can make beautiful maps

Made in R

Made in R

Made in R

Made in R

Made in R

Made in R

Made in R

And it's easy!

Map occurrences in 2 lines of code

plot(gmap(occdata, type = "satellite"))
points(occdata, col = "red", pch=20, cex = 2)

Let's go step by step

Dataframe with coordinates

species lon lat
1 Solanum acaule Bitter -66.10 -21.90
2 Solanum acaule Bitter -71.00 -13.50
52 Solanum acaule Bitter -66.43 -24.22
53 Solanum acaule Bitter -72.07 -13.35
54 Solanum acaule Bitter -68.97 -15.23
55 Solanum acaule Bitter -64.95 -17.75

Make it a spatial object

coordinates(occs) <- c("lon", "lat")
str(occs, 2)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 49 obs. of  1 variable:
  ..@ coords.nrs : int [1:2] 2 3
  ..@ coords     : num [1:49, 1:2] -66.1 -71 -66.4 -72.1 -69 ...
  .. ..- attr(*, "dimnames")=List of 2
  ..@ bbox       : num [1:2, 1:2] -72.5 -24.2 -64.7 -12.5
  .. ..- attr(*, "dimnames")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

Specify projection (CRS)

Project to Mercator and plot

plot(gmap(occs, type = "satellite"))
points(Mercator(occs), col = "red", pch = 20, cex = 2)

Alternatively, load shapefile

occs <- shapefile("data/occs.shp")
plot(gmap(occs, type = "satellite"))
points(Mercator(occs), col = "red", pch = 20, cex = 2)

Using ggmap

library(ggmap)
map <- get_map(bbox(occs), maptype = "watercolor", source = "stamen")
ggmap(map) +
  geom_point(aes(x = coords.x1, y = coords.x2), 
             data = as.data.frame(coordinates(occs)),
             colour = "red", size = 4)

Raster data

Download elevation data

elevation <- getData("alt", country = "ESP")
plot(elevation)

Using rasterVis

library(rasterVis)
levelplot(elevation)

Dynamic interactive maps (leaflet)

library(mapview)
mapView(occs)

Remote sensing too

Why doing GIS in R

  • Fully-reproducible scripts

  • Harness all R stats power

  • Data wrangling

  • Modelling

  • Dataviz

  • Easy!

Running GIS geoprocessing algorithms from R

  • RQGIS
  • rgrass7
  • RSAGA
  • ArcGIS

Some tutorials